package cn.hchaojie.camp.javafx.binding;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RadioButtonBinding extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(createContents());
		
		stage.setScene(scene);
		stage.show();
	}
	
	// 在这里创建页面布局
	private Parent createContents() {
		VBox vbox = new VBox(10);
		vbox.setPadding(new Insets(10));
		
		Label label = new Label("验证方式：");
		
		ToggleGroup group = new ToggleGroup();
		RadioButton rb1 = new RadioButton("邮箱");
		RadioButton rb2 = new RadioButton("手机号");
		rb1.setToggleGroup(group);
		rb2.setToggleGroup(group);
		
		Label label2 = new Label("手机号:");
		TextField tf = new TextField();
		
		// tf.disableProperty().bind(rb2.selectedProperty().not());
		tf.disableProperty().bind(Bindings.not(rb2.selectedProperty()));
		
		Button btn = new Button("验证：");
		btn.disableProperty().bind(tf.textProperty().isEmpty());

		vbox.getChildren().addAll(label, rb1, rb2, label2, tf, btn);

		return vbox;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
